<?php
/**
 * 装修公司
 */
namespace App\Http\ControllersAdmin;

use App\Service\MemberService;
use App\Validation\MemberValidation;
use App\Validation\MemberSearchValidation;
use App\Validation\DeleteValidation;

class MemberController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberSearchValidation, $this->request->getJsonRawBody());

		$data = MemberService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$avatar = $this->getFullUrl($value->avatar, FALSE, $this->config->imgurl);
			$value->avatar = $this->file_exists($avatar) ? $avatar : '';
			$d[$key] = $value;
		}
		$count = MemberService::getList($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	public function file_exists($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_NOBODY, 1); // 不下载
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		if(curl_exec($ch)!==false)
			return true;
		else
			return false;
	}


	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberValidation, $this->request->getJsonRawBody());
		$data = MemberService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$data = MemberService::getInfo($id);
		return $this->echoJson($data);
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new MemberValidation, $this->request->getJsonRawBody());
		$data = MemberService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [enableAction 启用 禁用]
	 * @return [type] [description]
	 */
	public function ableAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberService::able($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberService::delete($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [restoreAction 恢复]
	 * @return [type] [description]
	 */
	public function restoreAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberService::restore($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 彻底删除]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = MemberService::forever($raw->id);
		return $this->echoJson('ok');
	}
}